package com.mes.cloud.base.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.CodeMsg;
import com.enbatis.mybatisplugs.commons.utils.MD5Util;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.toolkit.LoginCacheUtil;
import com.mes.cloud.base.mapper.SysUserMapper;
import com.mes.cloud.base.service.SysUserService;
import com.mes.cloud.commons.entity.base.SysUser;
import com.mes.cloud.commons.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
* 描述：
* @author wwd
* @date 2019-12-01 19:30:50
*/
@Slf4j
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Override
    public ResultReturn login(SysUser user, HttpServletRequest request, HttpServletResponse response) {
        String username =request.getParameter("username");
        String code =request.getParameter("code");
        String codeCheck =request.getParameter("codeCheck");
        if (StringUtils.isBlank(codeCheck)){
            return ResultReturn.fail(CodeMsg.LOGIN_V_CODE_BLANK.getMessage());
        }

        if (!codeCheck.equalsIgnoreCase(code)){
            return ResultReturn.fail(CodeMsg.LOGIN_V_CODE_WRONG.getMessage());
        }
        if (null== LoginCacheUtil.mapVCode.get(code)){
            return ResultReturn.fail(CodeMsg.LOGIN_V_CODE_OVER.getMessage());
        }
        LoginCacheUtil.mapVCode.remove(code);

        String password= MD5Util.generate(request.getParameter("password"));

        List<SysUser> list=list(new Wrapper<>(new SysUser()).eq("username",username).eq("password",password));
        Optional<SysUser> first= list.stream().filter(dbUser->dbUser.getUsername().equals(user.getUsername())&&dbUser.getPassword().equals(password)).findFirst();

        if (first.isPresent()){
            user.setUsername(username);
           SysUser sysUser= first.get();
            Map<String,Object> userMap = new HashMap<>();
            userMap.put("userId",sysUser.getId()+"");
            userMap.put("name",sysUser.getName());
            userMap.put("username",sysUser.getUsername());
            userMap.put("effective", System.currentTimeMillis()+(120*60*1000));

            String token= JwtUtil.createJWT(userMap,JwtUtil.LOGIN_BASE);
            response.addHeader("Authorization",token);
            try {

            }catch (Exception e){
                log.error("登录失败"+e.getMessage(),e);
            }

            return  ResultReturn.success("/");

        }else{
            return ResultReturn.fail(CodeMsg.LOGIN_USER_PSD_ERROR.getMessage());
        }
    }
}
